﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.ViewModels;
using SYcwpb.Helpers;

namespace SYcwpb.Views
{
    /// <summary>
    /// StatisticSetting.xaml 的交互逻辑
    /// </summary>
    public partial class StatisticSetting : Window
    {
        public static StatisticSetting _view = null;
        private StatisticsSettingViewModel _viewModel;

        public static StatisticSetting GetInstance()
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
                _view = new StatisticSetting();

            return _view;
        }

        private StatisticSetting()
        {
            InitializeComponent();

            _viewModel = new StatisticsSettingViewModel();

            this.DataContext = _viewModel;

            int index = 0;

            if (CONSTDATA.UserModel.Line == "1")
                InitLine1(ref index);
            else
                InitLine2(ref index);

            //Grid.SetRow(operationStack, index);
            //Grid.SetColumnSpan(operationStack, 3);
        }

        private void InitLine1(ref int index)
        {
            Label lab_outmils = GetLabel("出段公里数");
            Label lab_inmils = GetLabel("入段公里数");
            Label lab_upToChangedMiles = GetLabel("上行至换班点公里数");
            Label lab_downToChangedMiles = GetLabel("下行至换班点公里数");
            Label lab_totalMiles = GetLabel("单趟公里数");

            TextBox txb_outmiles = GetTextBox("OutMiles");
            TextBox txb_inmiles = GetTextBox("InMiles");
            TextBox txb_upToChangedMiles = GetTextBox("UpToChangedMiles");
            TextBox txb_downToChangedMiles = GetTextBox("DownToChangedMiles");
            TextBox txb_totalMiles = GetTextBox("TotalMiles");

            StackPanel sta_outmiles = GetStackPanel(txb_outmiles, "公里");
            StackPanel sta_inmiles = GetStackPanel(txb_inmiles, "公里");
            StackPanel sta_upToChangedMiles = GetStackPanel(txb_upToChangedMiles, "公里");
            StackPanel sta_downToChangedMiles = GetStackPanel(txb_downToChangedMiles, "公里");
            StackPanel sta_totalMiles = GetStackPanel(txb_totalMiles, "公里");

            this.SetPosition(grid, index++, 0, lab_outmils);
            this.SetPosition(grid, index++, 0, lab_inmils);
            this.SetPosition(grid, index++, 0, lab_upToChangedMiles);
            this.SetPosition(grid, index++, 0, lab_downToChangedMiles);
            this.SetPosition(grid, index++, 0, lab_totalMiles);

            index = 0;

            this.SetPosition(grid, index++, 1, sta_outmiles);
            this.SetPosition(grid, index++, 1, sta_inmiles);
            this.SetPosition(grid, index++, 1, sta_upToChangedMiles);
            this.SetPosition(grid, index++, 1, sta_downToChangedMiles);
            this.SetPosition(grid, index++, 1, sta_totalMiles);
        }

        private void InitLine2(ref int index)
        {
            Label lab_outmils = GetLabel("出段公里数");
            Label lab_inmils = GetLabel("入段公里数");

            Label lab_upToChangedMiles = GetLabel("三台子上行至换班点公里数");
            Label lab_hkhupToChangedMiles = GetLabel("航空航大上行至换班点公里数");
            Label lab_downToChangedMiles = GetLabel("下行至换班点公里数");

            Label lab_totalMiles = GetLabel("全运路<->三台子单趟公里数");
            Label lab_hkhtotalMiles = GetLabel("全运路<->航空航大单趟公里数");

            TextBox txb_outmiles = GetTextBox("OutMiles");
            TextBox txb_inmiles = GetTextBox("InMiles");
            TextBox txb_upToChangedMiles = GetTextBox("UpToChangedMiles");
            TextBox txb_hkhupToChangedMiles = GetTextBox("HKHUpToChangedMiles");
            TextBox txb_downToChangedMiles = GetTextBox("DownToChangedMiles");
            TextBox txb_totalMiles = GetTextBox("TotalMiles");
            TextBox txb_hkhtotalMiles = GetTextBox("HKHTotalMiles");

            StackPanel sta_outmiles = GetStackPanel(txb_outmiles, "公里");
            StackPanel sta_inmiles = GetStackPanel(txb_inmiles, "公里");
            StackPanel sta_upToChangedMiles = GetStackPanel(txb_upToChangedMiles, "公里");
            StackPanel sta_hkhupToChangedMiles = GetStackPanel(txb_hkhupToChangedMiles, "公里");
            StackPanel sta_downToChangedMiles = GetStackPanel(txb_downToChangedMiles, "公里");
            StackPanel sta_totalMiles = GetStackPanel(txb_totalMiles, "公里");
            StackPanel sta_hkhtotalMiles = GetStackPanel(txb_hkhtotalMiles, "公里");


            this.SetPosition(grid, index++, 0, lab_outmils);
            this.SetPosition(grid, index++, 0, lab_inmils);
            this.SetPosition(grid, index++, 0, lab_upToChangedMiles);
            this.SetPosition(grid, index++, 0, lab_hkhupToChangedMiles);
            this.SetPosition(grid, index++, 0, lab_downToChangedMiles);
            this.SetPosition(grid, index++, 0, lab_totalMiles);
            this.SetPosition(grid, index++, 0, lab_hkhtotalMiles);

            index = 0;

            this.SetPosition(grid, index++, 1, sta_outmiles);
            this.SetPosition(grid, index++, 1, sta_inmiles);
            this.SetPosition(grid, index++, 1, sta_upToChangedMiles);
            this.SetPosition(grid, index++, 1, sta_hkhupToChangedMiles);
            this.SetPosition(grid, index++, 1, sta_downToChangedMiles);
            this.SetPosition(grid, index++, 1, sta_totalMiles);
            this.SetPosition(grid, index++, 1, sta_hkhtotalMiles);
        }

        private Label GetLabel(string content)
        {
            Label lab = new Label();
            lab.VerticalAlignment = VerticalAlignment.Center;
            lab.Content = content;

            return lab;
        }

        private TextBox GetTextBox(string bindingPath)
        {
            TextBox txb = new TextBox();
            txb.TextAlignment = TextAlignment.Right;
            txb.VerticalAlignment = VerticalAlignment.Center;
            txb.MaxLength = 7;
            txb.MinWidth = 120;

            Binding binding = new Binding(bindingPath);
            binding.Source = this.DataContext;
            binding.ValidatesOnDataErrors = true;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            BindingOperations.SetBinding(txb, TextBox.TextProperty, binding);

            BindingOperations.SetBinding(txb, TextBox.IsReadOnlyProperty, new Binding("ReadOnly") { Source = this.DataContext });

            return txb;
        }

        private StackPanel GetStackPanel(TextBox txb, string labelContent)
        {
            StackPanel stac = new StackPanel();
            stac.Orientation = Orientation.Horizontal;
            stac.Margin = new Thickness(10);

            stac.Children.Add(txb);
            stac.Children.Add(GetLabel(labelContent));

            return stac;

        }

        private void SetPosition(Grid grid, int row, int col, UIElement element)
        {
            grid.Children.Add(element);
            Grid.SetRow(element, row);
            Grid.SetColumn(element, col);
        }



    }
}
